package com.ebelter.temperaturegun.ui.activity;


import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.bluetooth.products.bracelet.bean.BraDb;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.activity_manager.ActivityManagers;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.dialogs.TextTipDialog;
import com.ebelter.temperaturegun.R;
import com.ebelter.temperaturegun.model.app.IConstant;
import com.ebelter.temperaturegun.model.app.TemUtils;
import com.ebelter.temperaturegun.model.http.TgUpLoadDataUtil;
import com.ebelter.temperaturegun.ui.view.TemTextTipDialog;
import com.raizlabs.android.dbflow.sql.language.Delete;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class UserInfoActivity extends TBaseActivity {
    public static final String TAG = "UserInfoActivity";
    private ImageView top_iv;
    private TextView top_tv1;
    private TextView uf_email_tv;
    private TextView uf_nickname_tv;

    @Override
    protected int setContentViewId() {
        return R.layout.activity_user_info;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        top_iv=findViewById(R.id.top_iv);
        top_tv1=findViewById(R.id.top_tv1);
        uf_email_tv=findViewById(R.id.uf_email_tv);
        uf_nickname_tv=findViewById(R.id.uf_nickname_tv);
        top_tv1.setText(R.string.zhhxx);
        ViewUtils.displayView(top_tv1);
        ViewUtils.displayView(top_tv1);

        top_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        updateLocalUI();
    }



    private void updateLocalUI(){
        ViewUtils.setTextViewStr(uf_nickname_tv, TemUtils.getUerNickName());
        ViewUtils.setTextViewStr(uf_email_tv, TemUtils.getUerEmail());
    }

    public void exitLogin(View view) {
        TemTextTipDialog textTipDialog=new TemTextTipDialog(this, StringUtils.getResStr(R.string.exit_tip));
        textTipDialog.setListener(new TemTextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                dialogView.cancel();
            }

            @Override
            public void ok(Dialog dialogView) {
                dialogView.cancel();
            //    UserSpUtil.remove(Constants.IUser.AUTH_KEY);
                UserSpUtil.writeLong(IConstant.IUserSpCon.USERID,-1);
                UserSpUtil.writeString(IConstant.IUserSpCon.PSW,null);
              //  UserSpUtil.remove(Constants.IUser.ehc_token);
             //   Delete.table(BraDb.class);//退出登录的时候删除手环同步过来的整张表

                Intent intent = new Intent(UserInfoActivity.this,LoginActivity.class);
                startActivity(intent);
                ActivityManagers.getInstance().closeActivity(MainActivity.TAG);
                ActivityManagers.getInstance().closeActivity(SettingActivity.TAG);
                finish();
            }
        });

        textTipDialog.show();

    }

    public void modifPsw(View view) {
        startActivity(ChangePasswordActivity1.class,null);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(CommonEventBus event) {
        if (!event.whichReceive.contains(TAG)) {
            return;
        }
        LogUtils.i(TAG, "UserInfoActivity--onEventMainThread接收到CommonEventBus信息--" + event.toString());
        if (event.cmd == ModifyNameActivity.ModifyName_Success){
            ViewUtils.setTextViewStr(uf_nickname_tv, TemUtils.getUerNickName());
        }



    }

    public void modifyname(View view) {
        startActivity(ModifyNameActivity.class,null);
    }
}
